package com.oystertech.sportcms.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.oystertech.sportcms.mapper.StatisticsMapper;
import com.oystertech.sportcms.model.entity.Athlete;
import com.oystertech.sportcms.model.entity.Team;
import com.oystertech.sportcms.model.vo.statistics.AthleteVO;
import com.oystertech.sportcms.service.StatisticsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表【athlete(运动员表)】服务接口实现
 *
 * @author Oyster Cheung
 * @since 2023-09-24 22:37:06
 */
@Service
public class StatisticsServiceImpl extends MPJBaseServiceImpl<StatisticsMapper, Athlete>
		implements StatisticsService {
	@Resource
	private StatisticsMapper statisticsMapper;

	@Override
	public List<AthleteVO> countAthleteByTeam(List<Long> teamIds) {
		MPJLambdaWrapper<Athlete> wrapper = new MPJLambdaWrapper<Athlete>()
				.select(Athlete::getTeamId)
				.selectCount(Athlete::getId, AthleteVO::getAthleteCount)
				.selectAssociation(Team.class, AthleteVO::getTeamInfo)
				.leftJoin(Team.class, Team::getId, Athlete::getTeamId)
				.in(Athlete::getTeamId, teamIds)
				.groupBy(Athlete::getTeamId);

		return statisticsMapper.selectJoinList(AthleteVO.class, wrapper);
	}
}
